﻿@using System.Reflection
@using System.Linq.Expressions
@using Whoever.Framework.Base;
@model Whoever.Patterns.Website2.Base.FieldModel
@{
    Type modelType = Model.ViewModel.GetType();
    Type entityModelType = Model.ViewModel.Entity.GetType();
    var expParameter = Expression.Parameter(Model.GetType(), "m");

    var modelProperty = Model.GetType().GetProperty("ViewModel");
    var expModelProperty = Expression.Property(expParameter, modelProperty);
    var expAsModelType = Expression.TypeAs(expModelProperty, modelType);
    var prop = Expression.Property(expAsModelType, Model.Property.Name);
}
@{
    //Seria bueno encontrar una mejor solucion a esto. Lo complicado es la definicion de la expression dependiendo del type de la property dinamicamente
    if (Model.Property.PropertyType == typeof(int))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, int>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(int?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, int?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(string))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, string>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(decimal))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, decimal>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(decimal?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, decimal?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(double))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, double>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(double?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, double?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(DateTime))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, DateTime>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }

    if (Model.Property.PropertyType == typeof(DateTime?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, DateTime?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.TextBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }
    if (Model.Property.PropertyType == typeof(bool))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.FieldModel, bool>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.CheckBoxFor(expLambda, Model.HTMLAttributes)
        </div>
    </div>
    </text>
    }   
}
